package com.amgl.mobike.module.search;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.text.TextUtils;

import com.amgl.mobike.MainActivity;
import com.amgl.mobike.R;
import com.amgl.mobike.app.IImageLoader;
import com.amgl.mobike.app.glide.ActivityImageLoader;
import com.amgl.mobike.base.BaseActivity;
import com.amgl.mobike.widget.GridDividerItemDecoration;
import com.amgl.mobike.widget.loadmore.ScrollToLastItemListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import timber.log.Timber;

public class ImageSearchActivity extends BaseActivity implements ISearchContract.ISearchView {

    private ISearchContract.ISearchPresenter mSearchPresenter;

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;

    @BindView(R.id.search_view)
    SearchView mSearchView;

    private ImageRVAdapter mRVAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    private IImageLoader mImageLoader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Timber.i("on create");
        setContentView(R.layout.activity_image_search);
        ButterKnife.bind(this);

        mSearchPresenter = new SearchPresenter(this);
        mImageLoader = new ActivityImageLoader(this);

        initView();

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }


    @Override
    protected void onRestart() {
        super.onRestart();
        Timber.i("on restart");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Timber.i("on start");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Timber.i("on resume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Timber.i("on pause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Timber.i("on stop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Timber.i("on destroy");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Timber.i("on save instance state");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Timber.i("on restore instance state");
    }

    private void initView() {
        mRVAdapter = new ImageRVAdapter(this, mImageLoader);
        mLayoutManager = new GridLayoutManager(this, 3);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mRVAdapter);

        //simple usage
        GridDividerItemDecoration dividerItemDecoration = new GridDividerItemDecoration(this, GridDividerItemDecoration.GRID_DIVIDER_VERTICAL);
        dividerItemDecoration.setVerticalDivider(getResources().getDrawable(R.drawable.divider_v));
        dividerItemDecoration.setHorizontalDivider(getResources().getDrawable(R.drawable.divider_h));


        mRecyclerView.addItemDecoration(dividerItemDecoration);

        mRecyclerView.addOnScrollListener(new ScrollToLastItemListener() {
            @Override
            protected void onScrollToLast(int lastVisiblePosition) {
                if (!isLoadingMore()
                        && lastVisiblePosition > 1) {
                    loadMore();
                }
            }
        });

        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Timber.d("on search action: %s", query);
                if (!TextUtils.isEmpty(query)) {
                    search(query);
                    return true;
                }
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });

        mSearchView.setIconifiedByDefault(true);
        mSearchView.setFocusable(true);
        mSearchView.setIconified(false);
        mSearchView.requestFocusFromTouch();

        search("mobike");
    }

    private boolean mIsLoadingMore = false;

    public synchronized boolean isLoadingMore() {
        return mIsLoadingMore;
    }


    String mQuery = "";

    private void search(String query) {
        if (TextUtils.isEmpty(query)) {
            return;
        }
        mQuery = query;
        mSearchPresenter.search(query);
    }

    private void loadMore() {
        if (TextUtils.isEmpty(mQuery)) {
            return;
        }
        mSearchPresenter.loadMore(mQuery);
    }

    @Override
    public void showImage(List<ImageData> imageDataList) {
        Timber.d("show image list");
        mRVAdapter.setDataList(imageDataList);
        mRVAdapter.notifyDataSetChanged();
    }

    @Override
    public void showImageMore(List<ImageData> imageDataList) {
        Timber.d("show image list more");
        mRVAdapter.addDataList(imageDataList);
        mRVAdapter.notifyDataSetChanged();
    }

    @Override
    public void showError(String message) {
        showToast(message);
    }

    @Override
    public void showRefreshing(boolean isRefreshing) {

    }

    @Override
    public void showLoadingMore(boolean isLoading) {
        mIsLoadingMore = isLoading;
    }
}
